/* ********************************************************************************************
*  Alvin Baldemeca
*  Tetris Project
*  Course: TCSS 305 , Alan Fowler
*  Term: Spring 2012
*  UNIVERISTY OF WASHINGTON TACOMA
*
*
**********************************************************************************************/

package view.container;

import controller.Board;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import model.AbstractPiece;
import model.Piece;


/**
 * This class is used to display the next Piece to be dropped in the board.
 * @author Alvin Baldemeca
 * @version 5/25/2012
 *
 */
@SuppressWarnings("serial")
public class TetrisPreviewPanel extends JPanel implements Observer
{

  /**
   * The width of the frame when it is initialized.
   */
  private static final int WIDTH = 50;
  
  /**
   * The height of the frame when it is initialized.
   */
  private static final int HEIGHT = 50;
 
  /**
   * The ratio need to set the offset when the panel is changes size.
   */
  private static final int ASPECT_RATIO = 8;
  
  /**
   *The x diviation location for the preview box.
   */
  private static final int X_DIVIATION = 3;
  /**
   *The y diviation location for the preview box.
   */
  private static final int Y_DIVIATION = 4;
  
  /**
   *Compensates for the required width to display the preview box.
   */
  private static final int WIDTH_COMPENSATION = 5;
  
  /**
   *Compensates for the required height to display the preview box. 
   */
  private static final int HIEGHT_COMPENSATION = 4;
  
  /**
   *The horizontal space offset to display the piece.
   */
  private static final int X_SPACE = 4;
  
  /**
   *   The vertical space offset to dispaly the piece.
   */
  private static final int Y_SPACE = 6;
  
  
  
  /**
   * The reference the tetris board that is being played.
   */
  private final Board my_board;
  
  /**
   * Constructs a TetrisPreiviewPanel.
   * @param the_board the tetris board to reference to, in order to get the next piece.
   */
  public TetrisPreviewPanel(final Board the_board)
  {
    super();
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setBackground(Color.BLUE);
    my_board = the_board;
  }
  
  @Override
  public void paintComponent(final Graphics the_graphics) 
  {
    super.paintComponent(the_graphics);
    final Graphics2D g2 = (Graphics2D) the_graphics;
    paintNextComponet(g2);    
  }
  
  /**
   * This is a private method used to paint the next tetris piece to be dropped on
   * the board. 
   * @param the_graphics the Graphics2D to paint the next tetris piece to
   */
  private void paintNextComponet(final Graphics2D the_graphics)
  {
    if (!my_board.isGameOver())
    { 
      final Rectangle rect = the_graphics.getClipBounds();
      int size = rect.height;
      if (rect.width < rect.height / 2)
      {
        size = rect.width;
      }
      
      final int offset = size / ASPECT_RATIO; 
      
  
  
      the_graphics.setColor(getBackground());
      the_graphics.fillRect(rect.x, rect.y, rect.width, rect.height);     
      the_graphics.setColor(Color.BLACK);
      the_graphics.fillRect(offset * X_DIVIATION, offset * Y_DIVIATION, 
                            offset * WIDTH_COMPENSATION, offset * HIEGHT_COMPENSATION);
  
      final Piece next_piece = my_board.getNextPiece();
      final int[][] tetris_piece = ((AbstractPiece) next_piece).getRotation();
      
      for (int i = 0; i < tetris_piece.length; i++)
      {
        the_graphics.setColor(Color.GREEN);
        the_graphics.fill3DRect((tetris_piece[i][0] + X_SPACE) * offset, 
                               (Y_SPACE - tetris_piece[i][1]) * offset , 
                               offset, offset, true);
      }
      
    }
    
    
  }
  
  
  @Override
  public void update(final Observable the_first_argument, final Object the_second_argument)
  {
    repaint();
  }
}
